﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Utils;
using Shpm.App.Input.Bean;
using Shpm.System;
using Shpm.App.Common.Dao;
using Shpm.App.Common.Bean;

public partial class App_Input_EployeeDinnerUpdForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Label1.Text = this.hidWeek.Value;




        if (!IsPostBack)
        {
            EmployeeDinnerDao dao = new EmployeeDinnerDao();

            //Context.Items["DINNER_ID"] = 1;
            //
            ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
            ViewState["EMPLOYEE_NAME"] = Context.Items["EMPLOYEE_NAME"];
            ViewState["DINNER_DATE"] = Context.Items["DINNER_DATE"];

            //drop绑定
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            paramBean.Catalog = "M_DISH.DISH_TIME";
            IList dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropDinnerType.DataSource = dataList;
            this.dropDinnerType.DataBind();
            dataList.RemoveAt(0);
            this.dropDinnerType2.DataSource = dataList;
            this.dropDinnerType2.DataBind();

            paramBean.Catalog = "M_DISH.DISH_TYPE";
            dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropType.DataSource = dataList;
            this.dropType.DataBind();


            paramBean.Catalog = "M_DISH.DISH_CLASS";
            dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropClass.DataSource = dataList;
            this.dropClass.DataBind();

            //编辑
            if (Context.Items.Contains("DINNER_ID"))
            {
                //送餐信息
                this.labDinnerId.Text = Convert.ToString(Context.Items["DINNER_ID"]);
                EmployeeDinnerBean edBean = new EmployeeDinnerBean();
                edBean.DinnerId = Convert.ToInt32(Context.Items["DINNER_ID"]);
                edBean =dao.GetEmployeeDinner(edBean);
                this.txtEmpId.Text = edBean.EmployeeId;
                this.txtMemo.Text = edBean.DinnerMemo;
                this.dropDinnerType2.SelectedValue = edBean.DinnerType;

                EmployeeBean eBean =new EmployeeBean();
                EmployeeDao eDao = new EmployeeDao();
                eBean.EmployeeId = edBean.EmployeeId;
                eBean = eDao.GetEmployee(eBean);
                this.txtEmpName.Text = eBean.EmployeeName;


                //菜单信息
                EmployeeDinnerDetailBean eddBean = new EmployeeDinnerDetailBean();
                eddBean.DinnerId = Convert.ToInt32(Context.Items["DINNER_ID"]);
                IList eddList = dao.ListEmployeeDinnerDetails(eddBean);
                this.GridView2.DataSource = eddList;
                this.GridView2.DataBind();
                ViewState["DISH_LIST"] = eddList;
                btnOrder.Enabled = false;
            }
            //新建
            else
            {
                this.GridView2.DataSource = null;
                this.GridView2.DataBind();
                btnOrder.Enabled = true;
            }

            //
            this.txtDate.Text = DateUtils.convertToString(DateTime.Now);
            string weekstr = DateTime.Now.DayOfWeek.ToString();
            int week = 0;
            switch (weekstr)
            {
                case "Monday": weekstr = "星期一"; week = 1; break;
                case "Tuesday": weekstr = "星期二"; week = 2; break;
                case "Wednesday": weekstr = "星期三"; week = 3; break;
                case "Thursday": weekstr = "星期四"; week = 4; break;
                case "Friday": weekstr = "星期五"; week = 5; break;
                case "Saturday": weekstr = "星期六"; week = 6; break;
                case "Sunday": weekstr = "星期日"; week = 0; break;
            }

            this.LabDate.Text = DateUtils.convertToString(DateTime.Now) + "(" + weekstr + ")";
            this.hidWeek.Value = weekstr;
            this.Label1.Text = weekstr;
            this.LabTime.Text = "全部";


            

            //菜单列表

            BookFoodDao bfDao = new BookFoodDao();
            DishBean bean = new DishBean();
            bean.MenuDate = DateTime.Now;
            IList list = bfDao.FindDish(bean);
            if (list.Count <= 0)
            {
                bean.MenuDate = null;
                bean.MenuWeek = week;
                list = bfDao.FindDish(bean);
            }
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();


            txtEmpId.Attributes.Add("onblur", "showEmployeeName(this.id,'" + txtEmpName.ClientID + "');");
            txtEmpName.Attributes.Add("onfocus", "document.getElementById('" + txtEmpId.ClientID + "').focus();");
        }
        this.Literal1.Text = "";
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }

        BookFoodDao dao = new BookFoodDao();
        DishBean bean = new DishBean();
        bean.MenuDate = DateUtils.convertToDate(this.txtDate.Text);
        bean.DishType = string.IsNullOrEmpty(this.dropType.SelectedValue) ? null : this.dropType.SelectedValue;
        bean.DishClass = string.IsNullOrEmpty(this.dropClass.SelectedValue) ? null : this.dropClass.SelectedValue;
        this.LabDate.Text = this.txtDate.Text + "(" + this.hidWeek.Value + ")";

        if (!string.IsNullOrEmpty(this.dropDinnerType.SelectedValue))
        {
            bean.MenuTime = this.dropDinnerType.SelectedValue;
            this.LabTime.Text = this.dropDinnerType.SelectedItem.Text;
        }
        else
        {

            this.LabTime.Text = "全部";
        }
        ViewState["MEMU_DATE"] = bean.MenuDate;
        ViewState["TYPE"] = bean.DishType;
        ViewState["CLASS"] = bean.DishClass;
        ViewState["TIME"] = bean.MenuTime;
        IList list = dao.FindDish(bean);
        if (list.Count <= 0)
        {
            bean.MenuDate = null;

            string weekstr = Convert.ToDateTime(this.txtDate.Text).DayOfWeek.ToString();
            int week = 0;
            switch (weekstr)
            {
                case "Monday": week = 1; break;
                case "Tuesday": week = 2; break;
                case "Wednesday": week = 3; break;
                case "Thursday": week = 4; break;
                case "Friday": week = 5; break;
                case "Saturday": week = 6; break;
                case "Sunday": week = 0; break;
            }
            bean.MenuWeek = week;
            list = dao.FindDish(bean);
        }
        this.GridView1.PageIndex = 0;
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

    protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        //取得物资申领单内容
        IList list = null;
        if (ViewState["DISH_LIST"] == null)
        {
            list = new ArrayList();
        }
        else
        {
            list = (IList)ViewState["DISH_LIST"];
        }


        int dishId = Convert.ToInt32((sender as GridView).DataKeys[e.NewEditIndex].Values[0]);


        //取得前一画面的申领单数据

        EmployeeDinnerDetailBean  paramBean = null;

        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            paramBean = (EmployeeDinnerDetailBean)list[i];
            paramBean.DishQuantity = Convert.ToInt32((this.GridView2.Rows[i].FindControl("quantity") as TextBox).Text);
        }

        //查找单个菜肴信息
        EmployeeDinnerDao dao = new EmployeeDinnerDao();
        EmployeeDinnerDetailBean bean = new EmployeeDinnerDetailBean();
        bean.DishId = dishId;
        bean = dao.GetEmployeeDinnerDetail(bean);

        //判断是否加入新的物资

        bool flg = true; //true:新物资；false:旧物资
        for (int i = 0; i < list.Count; i++)
        {
            paramBean = (EmployeeDinnerDetailBean)list[i];
            if (bean.DishId == paramBean.DishId)
            {
                paramBean.DishQuantity = paramBean.DishQuantity == 99999 ? paramBean.DishQuantity : paramBean.DishQuantity + 1;
                flg = false;
                break;
            }
        }

        if (flg)
        {
            //MemberDinnerDetailBean dishBean = new MemberDinnerDetailBean();
            //dishBean.DishId = bean.DishId;
            //dishBean.DishName = bean.DishName;
            //dishBean.DishPrice = bean.DishPrice;
            bean.DishQuantity = 1;
            list.Add(bean);
        }




        ViewState["DISH_LIST"] = list;
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            EmployeeDinnerDetailBean bean = (EmployeeDinnerDetailBean)e.Row.DataItem;
            TextBox quantity = ((TextBox)e.Row.FindControl("quantity"));
            quantity.Text = Convert.ToString(bean.DishQuantity);
            quantity.Attributes.Add("onblur", "checkQuantity('" + quantity.ClientID + "')");
            quantity.Attributes.Add("onfocus", "tempDate('" + quantity.ClientID + "')");
            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);


        }
    }
    protected void GridView2_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        IList list = (ArrayList)ViewState["DISH_LIST"];
        int row = e.RowIndex;
        list.RemoveAt(row);
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);


        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Context.Items["EMPLOYEE_ID"] = this.ViewState["EMPLOYEE_ID"];
        Context.Items["EMPLOYEE_NAME"] = this.ViewState["EMPLOYEE_NAME"];
        Context.Items["DINNER_DATE"] = this.ViewState["DINNER_DATE"];
        string url = "~/App/Input/EmployeeDinnerSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);   
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       
    }
    private bool checkEmpId()
    {
        EmployeeBean eBean = new EmployeeBean();
        EmployeeDao eDao = new EmployeeDao();
        eBean.EmployeeId = txtEmpId.Text;
        eBean = eDao.GetEmployee(eBean);
        if (eDao.GetEmployee(eBean) == null)
            return false;
        return true;
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {

        IList list = (IList)ViewState["DISH_LIST"];
        EmployeeDinnerDao Dao = new EmployeeDinnerDao();
        if (checkEmpId() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtEmpId.Text);
            return;
        }
        if (list == null || list.Count <= 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                 HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0094").ToString().Replace("{0}", "菜单内容"); ;
            return;
        }
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;

        EmployeeDinnerBean mdBean = new EmployeeDinnerBean();
        mdBean.EmployeeId = txtEmpId.Text;
        mdBean.DinnerMemo = this.txtMemo.Text;
        mdBean.DinnerType = this.dropDinnerType2.SelectedValue;
        mdBean.UserId = loginId;
        //编辑
        if (!string.IsNullOrEmpty(this.labDinnerId.Text))
        {
            mdBean.DinnerId = Convert.ToInt32(this.labDinnerId.Text);
            Dao.Upd((IList)ViewState["DISH_LIST"], mdBean);
            ((Design_MasterPage)this.Master).InfoMessage =
                           HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
        //新建
        else
        {
            this.labDinnerId.Text = Convert.ToString(Dao.Insert((IList)ViewState["DISH_LIST"], mdBean));
            ((Design_MasterPage)this.Master).InfoMessage =
                           HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            Context.Items["EMPLOYEE_ID"] = this.ViewState["EMPLOYEE_ID"];
            Context.Items["EMPLOYEE_NAME"] = this.ViewState["EMPLOYEE_NAME"];
            Context.Items["DINNER_DATE"] = this.ViewState["DINNER_DATE"];
            string url = "~/App/Input/EmployeeDinnerSrhForm.aspx";
            ControllerUtils.Transfer(Context, url, true);   
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        //从pop画面添加菜肴
        if (!string.IsNullOrEmpty(this.hidFoodsId.Value))
        {
            //取得物资申领单内容
            IList list = null;
            string[] foodsId = this.hidFoodsId.Value.Split(',');
            if (ViewState["DISH_LIST"] == null)
            {
                list = new ArrayList();
            }
            else
            {
                list = (IList)ViewState["DISH_LIST"];
            }


            for (int j = 0; j < foodsId.Length / 2; j++)
            {
                int dishId = Convert.ToInt32(foodsId[j * 2]);


                //取得前一画面的申领单数据

                EmployeeDinnerDetailBean paramBean = null;

                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    paramBean = (EmployeeDinnerDetailBean)list[i];
                    paramBean.DishQuantity = Convert.ToInt32((this.GridView2.Rows[i].FindControl("quantity") as TextBox).Text);
                }

                //查找单个菜肴信息
                EmployeeDinnerDao dao = new EmployeeDinnerDao();
                EmployeeDinnerDetailBean bean = new EmployeeDinnerDetailBean();
                bean.DishId = dishId;
                bean = dao.GetEmployeeDinnerDetail(bean);

                //判断是否加入新的物资

                bool flg = true; //true:新物资；false:旧物资
                for (int i = 0; i < list.Count; i++)
                {
                    paramBean = (EmployeeDinnerDetailBean)list[i];
                    if (bean.DishId == paramBean.DishId)
                    {
                        paramBean.DishQuantity = paramBean.DishQuantity == 99999 ? paramBean.DishQuantity : paramBean.DishQuantity + 1;
                        flg = false;
                        break;
                    }
                }

                if (flg)
                {
                    //MemberDinnerDetailBean dishBean = new MemberDinnerDetailBean();
                    //dishBean.DishId = bean.DishId;
                    //dishBean.DishName = bean.DishName;
                    //dishBean.DishPrice = bean.DishPrice;
                    bean.DishQuantity = 1;
                    list.Add(bean);
                }




                ViewState["DISH_LIST"] = list;
                this.GridView2.DataSource = list;
                this.GridView2.DataBind();

                this.hidFoodsId.Value = "";

            }


        }
        if (GridView2.Rows.Count > 0)
        {
            decimal dSum = 0;
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                decimal dPrice = Convert.ToDecimal(GridView2.Rows[i].Cells[4].Text);
                TextBox quantity = ((TextBox)GridView2.Rows[i].FindControl("quantity"));
                int iQuantity = Convert.ToInt32(quantity.Text);
                dSum = dSum + (dPrice * iQuantity);
            }
            lblSum.Text = "小计：" + dSum + "元";
        }
        
        if (GridView2.Rows.Count > 0)
        {
            hidPrice.Value = "";
            hidCid.Value = "";
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                if (hidPrice.Value == "")
                {
                    hidPrice.Value = Convert.ToString(GridView2.Rows[i].Cells[4].Text);
                }
                else
                {
                    hidPrice.Value = hidPrice.Value + "," + Convert.ToString(GridView2.Rows[i].Cells[4].Text);
                }
                
                TextBox quantity = ((TextBox)GridView2.Rows[i].FindControl("quantity"));

                if (hidCid.Value == "")
                {
                    hidCid.Value = quantity.ClientID;
                }
                else
                {
                    hidCid.Value = hidCid.Value + "," + quantity.ClientID;
                }
            }
        }
    }
}
